package com.guangyuan.network.mvc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guangyuan.network.constants.ProductGroupConstant;
import com.guangyuan.network.dataobject.ProductAttr;
import com.guangyuan.network.external.OperatorFactory;

public class ProductGroupAttrAction implements Action {

	private boolean updateProductAttr(ProductAttr pA) {
		return OperatorFactory.getProductAttrOper().updateProductAttr(pA);
	}

	private boolean addProductAttr(ProductAttr pA) {
		return OperatorFactory.getProductAttrOper().insertProductAttr(pA);
	}

	public boolean addOrUpdateProductAttr(HttpServletRequest request,
			HttpServletResponse response) {
		ProductAttr pA = getProductAttr(request, response);
		if (null != pA.getId() && 0 != pA.getId()) {
			return updateProductAttr(pA);
		} else {
			return addProductAttr(pA);
		}
	}

	private ProductAttr getProductAttr(HttpServletRequest request,
			HttpServletResponse response) {
		ProductAttr productAttr = new ProductAttr();
		String idStr = request.getParameter(ProductGroupConstant.attr_id);
		int id = Integer.parseInt((null != idStr && !"".equals(idStr) ? idStr
				: "0"));
		productAttr.setId(id);
		productAttr.setName(request
				.getParameter(ProductGroupConstant.attr_name));
		productAttr.setCnName(request
				.getParameter(ProductGroupConstant.attr_cnName));
		productAttr.setDescription(request
				.getParameter(ProductGroupConstant.attr_description));
		// FIXME! 修改操作者的名称
		productAttr.setOperator("吴天翼");
		productAttr.setProductGroupId(Integer.parseInt(request.getParameter(ProductGroupConstant.GROUP_ID)));
		return productAttr;
	}
}
